<?php
/**
 * Created by PhpStorm.
 * User: xudongsheng
 * Date: 2017/9/17
 * Time: 上午12:18
 */

namespace app\admin\controller;


use app\admin\model\File;
use think\Request;
use think\Response;

class Files extends Common
{
    public function uploads(){
        // 根据上传过来的 img 类型 来 判断
        // input('post.mime') ...
//        var_dump(input('post.rule'));die;
        return  json((new File())->uploads(input('post.rule')));
    }
    public function editor(){
        // 根据上传过来的 img 类型 来 判断
        // input('post.mime') ...
        $result =  (new File())->uploads(input('post.rule'));
        return json([
            'success'   =>  $result['upload_file']['error']?false:true,
            'msg'       =>  isset($result['upload_file']['msg'])?$result['upload_file']['msg']:'',
            'file_path' =>  isset($result['upload_file']['data'])?$result['upload_file']['data']['save_path']:''
        ]);
    }

    public function lists(){
        $this->assign('file',File::paginate(15));
        return $this->fetch();
    }

    public function delFile($file_id){
        ($nowFile =  File::get($file_id)) OR $this->error('没有此文件,无法删除！');
        if (is_file($nowFile['save_path'])){
            @unlink($nowFile['save_path']);
        }
        if ($nowFile->delete()){
            $this->success('删除成功！');
        }
        $this->success('删除失败！');
    }
}